package demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import lib.ConnectionWeb;
import lib.StringEdit;

import org.htmlcleaner.CleanerProperties;
import org.htmlcleaner.HtmlCleaner;
import org.htmlcleaner.TagNode;
import org.htmlcleaner.XPatherException;

import pojo.Link;
import pojo.Website;
import dao.LinkDAO;

public class getBook {

	/**
	 * @param args
	 * @throws XPatherException 
	 */
	public static void main(String[] args) throws XPatherException {
		// TODO Auto-generated method stub
		String str4= "/html/body/table/tbody/tr/td/div[3]/table/tbody/tr";
					  
		str4= StringEdit.EditXpath(str4);
		String str5="/html/body/table/tbody/tr/td/div[10]";
		str5= StringEdit.EditXpath(str5);
		LinkDAO dao = new LinkDAO();
		
		List<Link> list= dao.getList();
		List<?> list1;
		ConnectionWeb con= new ConnectionWeb();
		
		String xpath2="/html/body/div/div[5]/table/tbody/tr/td[2]/div[4]/table/tbody/tr/td[2]/div";
		
		for(int i=0; i<list.size();i++)
		{
			Link t= list.get(i);
			String url = t.getLinkUrl();
			TagNode root1= con.getInfo(url);
			List<String> list_link=new ArrayList<String>();

			if(root1==null)
			{
				
			}
			else
			{	String xpath= t.getWebsite().getPage();
			    xpath=StringEdit.EditXpath(xpath);
			    String xpath_content= t.getWebsite().getBookContent();
			    xpath_content=StringEdit.EditXpath(xpath_content);
				Object[] list_node = root1.evaluateXPath(xpath);
				if(list_node.length>0)
				{
					if(list_link!=null) list_link.clear();
					
				//	list_link.add(url);
					 TagNode root2= (TagNode) list_node[0];
					 list1= root2.getChildTagList();
					 String link_hien_tai=url;
					 String link_ke_tiep=null;
					 int index=0;		
					 String trang_hien_tai="1";
					 
					 boolean flag=false; 
					 
					/* Object[] list_content=root1.evaluateXPath(xpath_content);
					 if(list_content.length>0)
					 {
						 TagNode rootContent = (TagNode) list_content[0];
						 List<?> list_node_content=rootContent.getElementListByName("a", true);
						 String link_content1="";
						 String link_content2="";
						 for(int j=0;i<list_node_content.size();j++)
						 {
							 TagNode node_content= (TagNode) list_node_content.get(j);
							 link_content2=node_content.getAttributeByName("href");
							 if(link_content2!=null&&link_content2.equals(link_content1)==false)
							 { 	 link_content1=link_content2; 
								 String st=link_content2;
								 if(link_content2.indexOf("http")==-1) link_content2=url+link_content2;
								 
								
								 TagNode book = con.getInfo(link_content2);
								 if(book==null)
								 {
									 link_content2=t.getWebsite().getWebsite()+st;
									 book = con.getInfo(link_content2);
								 }
								 
								 Object[] list_book_content=book.evaluateXPath(str4);
								 if(list_book_content.length>0)
								 {
									 TagNode root_book =(TagNode)list_book_content[0];
									 List<?>l_book_content= root_book.getAllElementsList(true);
									 for(int k=0;k<l_book_content.size();k++)
									 {
										 TagNode t_book= (TagNode) l_book_content.get(k);
										 t_book.getChildTagList();
									 }
								
								 }
							
							 }
						 }
					 }
					 */
					 while(flag==false)
					 {		
						 list_link.add(link_hien_tai);
						 for(int j=0; j< list1.size();j++)
						 {
							 TagNode t1=(TagNode) list1.get(j);
							 String sNum= t1.getText().toString();
							 sNum= StringEdit.ChuanHoa(sNum);
							 if(sNum.equals(trang_hien_tai)==true)
							 {
								if(j<list1.size()-1)
								{
									TagNode t2=(TagNode) list1.get(j+1);
									List<?> list2=t2.getElementListByName("a", true);
									if(list2.size()!=0||t2.getName().equals("a")==true)
									{
										TagNode t3 ;
										if(list2.size()>0)
											t3= (TagNode) list2.get(0);
										else t3=t2;
										link_ke_tiep=t3.getAttributeByName("href");
										trang_hien_tai=t3.getText().toString();
										break;
									}
									else
										flag= true;
								}
							 }
							 
						 }
						 
						 if(flag==true) break;
						 if(link_ke_tiep.indexOf("http")==-1)
							 link_ke_tiep=url+link_ke_tiep;
						 link_hien_tai=link_ke_tiep;
						 
						TagNode troot= con.getInfo(link_ke_tiep);
						Object[] t_list_node = troot.evaluateXPath(xpath);
						if(t_list_node.length>0)
						{
							TagNode temp=(TagNode) t_list_node[0];
							list1= temp.getChildTagList();
						}
						
						System.out.println(trang_hien_tai);
						if(trang_hien_tai.equals("18")==true)
						{
							System.out.print("a");
						}
						
					 }
					 

					
				}
				else
				{
					
				}
			}
		}
		
		
	}

	public static boolean isNumeric(String str)  
	{  
	  try  
	  {  
	    double d = Double.parseDouble(str);  
	  }  
	  catch(NumberFormatException nfe)  
	  {  
	    return false;  
	  }  
	  return true;  
	}
}
